<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>角色管理</title>
    <link rel="stylesheet" href="/webjars/layui/2.5.5/css/layui.css"  media="all">
</head>
<body class="pear-container">

<div class="layui-card">
    <div class="layui-card-body">
        <button type="button" class="layui-btn layui-btn-normal layui-btn-sm" lay-event="ShouQuan" onclick="ShouQuan()">角色授权</button>
        <table id="role-table" lay-filter="role-table"></table>
    </div>

</div>

<div id="tree" style="display: none"></div>


<script src="/webjars/layui/2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['table','form','jquery'],function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.$;


        table.render({
            id: "role-table",
            elem: "#role-table",
            url: "/xiTongGuAnLi/ChaJueSe",
            cols: [
                [
                    {type:'radio'},
                    {title: 'id', field: 'id', align:'center'},
                    {title: '角色名', field: 'roleName', align:'center'},
                    {title: '更新时间', field: 'lastUpdateTime', align:'center'}
                ]
            ]
        })

    })
    function ShouQuan() {
        layui.use(['table','form','jquery','layer','tree'],function () {
            let layer=layui.layer;
            let table=layui.table;
            let tree=layui.tree;
            let $=layui.$;
            let arr=table.checkStatus("role-table").data;
            console.log(arr.length)
            if(arr.length==0){
                layer.alert("请选择一个授权");
                return;
            }
            let roleId=arr[0].id;
            $.get(`/JueSeShouQuan/searchTreeData?roleId=${arr[0].id}`)
                .done(({data})=>{
                    console.log(data)
                    tree.render({
                        id:"tree",
                        elem:"#tree",
                        showCheckbox:true,
                        data:data
                    });
                    layer.open({
                        type: 1,
                        offset:'50px',
                        skin:'layui-layer-molv',
                        area:["500px","550px"],
                        content:layui.$("#tree"),
                        btn:["确定","取消"],
                        yes(){
                            let nodes=tree.getChecked("tree").flat(Infinity);
                            let ids=nodes.map(node=>node.id);
                            let ShuZu=new Array();
                            for (let i=0;i<nodes.length;i++){
                                let aa=nodes[i].id;
                                ShuZu.push(aa);
                                for (let j=0;j<nodes[i].children.length;j++){
                                    let a2 =nodes[i].children[j].id;
                                    ShuZu.push(a2)
                                }
                            }
                            console.log(ShuZu);
                            // let ida=nodes.map(node=>node.children.id);
                            // console.log(nodes.children.id)
                            // console.log(nodes);
                            // console.log(ids+"aa"+ida)

                            $.post("/JueSeShouQuan/grant",`roleId=${roleId}&functionid=${ShuZu}`)
                                .done(({msg})=>{
                                    layer.close(layer.index);
                                    alert("授权成功")
                                });
                        }
                    })
                })

        })
    }
</script>
</body>
</html>
